var app = getApp();
var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');

Page({
  data: {
    shopShareImg: "",
    StorageView: api.StorageView
  },
  onLoad: function (e) {
    this.createShareImg();
  },
  onReady: function () { },
  onShow: function () {
  },
  click: function () {
  },
  onHide: function () { },
  onUnload: function () { },
  onPullDownRefresh: function () { },
  onReachBottom: function () { },
  onShareAppMessage: function () {
    var userInfo = wx.getStorageSync('userInfo');
    var path = "";
    if (userInfo != null) {
      path = '/pages/index/index?userId=' + userInfo.userId;
    } else {
      path = '/pages/index/index?userId=' + userInfo.userId;
    }
    return {
      title: '您身边的智能机器人一站式购物平台！',
      desc: '小哈微信小程序商城',
      path: path
    }
  },
  createShareImg: function () {
    var b = this;
    wx.showLoading({
      title: "图片生成中",
      mask: !0
    });
    util.request(api.getCodeUnlimit).then(function (res) {
      wx.hideLoading()
      if (res.errno == 0) {
        b.setData({
          shopShareImg: api.WxApiView+res.data.url
        });
      }else{
        wx.showToast("图片生成失败," + res.errmsg)
      }
    }).catch(function (e) {
      wx.hideLoading();
      wx.showToast("图片生成失败")
    });
  }, 
  saveImg: function (b) {
    var c = this, d = b.currentTarget.dataset.url;
     wx.showLoading({
       title: "正在保存"
     });
     this.saveImgToPhoto();
  },
  saveImgToPhoto: function () {
    var a = this;
    wx.getSetting({
      success: function (b) {
        return b.authSetting["scope.writePhotosAlbum"] ? a.save() : void wx.authorize({
          scope: "scope.writePhotosAlbum",
          success: function () {
            a.save();
          },
          fail: function () {
            wx.showModal({
              title: "提示",
              content: "您未授权保存到相册功能，将无法保存到相册，建议您打开保存到相册授权",
              success: function (b) {
                return b.confirm ? wx.openSetting({
                  success: function (b) {
                    b.authSetting["scope.writePhotosAlbum"] ? a.save() : a.triggerEvent("err", {
                      value: "授权失败"
                    });
                  },
                  fail: function () {
                    a.triggerEvent("err", {
                      value: "调用设置失败"
                    });
                  }
                }) : void a.triggerEvent("cancel");
              }
            });
          }
        });
      },
      fail: function () {
        a.triggerEvent("err", {
          value: "设置失败"
        });
      }
    });
  },
  save: function () {
    var a = this;
    console.log(a.data.shopShareImg);
    wx.getImageInfo({
      src: a.data.shopShareImg,
      success: function (b) {
        console.log(b);
        wx.saveImageToPhotosAlbum({
          filePath: b.path,
          success: function (e) {
            wx.hideLoading();
            wx.showToast({
              icon: "success",
              title: "保存成功"
            });
          },
          fail: function (e) {
            wx.hideLoading()
            wx.showToast({
              icon: "none",
              title: "保存失败"
            });
          }
        });
      },
      fail: function (e) {
        wx.hideLoading()
        wx.showToast({
          icon: "none",
          title: "保存失败:" + e.errMsg
        });
      }
    });
  }
})